package org.yu.mpl.junior01.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * @classDesc: 功能描述: 元数据处理器
 * @author: ywh
 */
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
//        strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now());
        // 优化: 有没有这个属性,有的话才自动填充
        boolean createTime = metaObject.hasSetter("createTime");
        if (createTime) {
            strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now());
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
//        strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
        // 优化:  只有这个为null的时候才进行自动填充
        Object updateTime = getFieldValByName("updateTime", metaObject);
        if (ObjectUtils.isEmpty(updateTime)) {
            strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
        }
    }
}
